# coding= utf-8
import requests
import base64
import demjson
import json

global null
null = ''
api_key = 'M8Ss2ZvCNtn7TSmxpCpkKOnC'
secret_key = 'a0uKvNlHHrO7hfUKzdpo9cKG79EsrEZV'
url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + str(api_key) + '&client_secret=' + str(secret_key)
res = eval(requests.get(url).text)
access_token = res['access_token']
face_search = 'https://aip.baidubce.com/rest/2.0/face/v3/search' + '?access_token=' + str(access_token)
header = {
    'Content-Type':'application/json'
}
data = {
    "image_type":"BASE64",
    "group_id_list":"group_1",  #这个要在人脸识别的应用里创建人脸库，并添加相应图片
    #这里弄了几个明星的照片
}
with open('lc2.jpg', 'rb') as f:
    image = base64.b64encode(f.read())
    data["image"] = str(image).encode('utf-8')

res2 = eval(requests.post(url=face_search, data=data, headers=header).text)
#转成dict

# print(res2)
# res2 =demjson.encode(res2)

print ("识别结果：", res2["result"]["user_list"][0]["user_id"])
print ("准确率：", res2["result"]["user_list"][0]["score"])